﻿using Assets.AnimationPlayer.Scripts.UI;
using UnityEngine;

namespace Assets.AnimationPlayer.Scripts.Util
{

    public class YioksUiAuto:MonoBehaviour
    {
        #region 界面自适应
        public void UiAuto()
        {
            if (_uiAutoStatus)
            {
                return;
            }

            _uiAutoStatus = true;
            transform.GetComponent<RectTransform>().sizeDelta = transform.GetComponent<RectTransform>().sizeDelta * AnimationVar.ScaleNum;
            transform.GetComponent<RectTransform>().anchoredPosition = transform.GetComponent<RectTransform>().anchoredPosition * AnimationVar.ScaleNum;
            _uiAuto(transform);
        }

        private static void _uiAuto(Transform mytf)
        {
            foreach (Transform tf in mytf)
            {
                try
                {
                    tf.GetComponent<RectTransform>().sizeDelta = tf.GetComponent<RectTransform>().sizeDelta*
                                                                 AnimationVar.ScaleNum;
                    tf.GetComponent<RectTransform>().anchoredPosition =
                        tf.GetComponent<RectTransform>().anchoredPosition*AnimationVar.ScaleNum;

                    if (tf.GetComponent<UnityEngine.UI.Text>() != null)
                    {
                        tf.GetComponent<UnityEngine.UI.Text>().fontSize =
                            int.Parse(
                                (tf.GetComponent<UnityEngine.UI.Text>().fontSize*AnimationVar.ScaleNum)
                                    .ToString("f0"));
                    }
                    _uiAuto(tf);
                }
                catch
                {
                    //
                }
            }
        }
        #endregion

        private bool _uiAutoStatus;
        internal void Update()
        {
            if (!_uiAutoStatus)
            {
                UiAuto();
            }
        }
    }
}